package com.xinsoft.controller.qua;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.QuaPatrolPlanDTO;
import com.xinsoft.entity.dto.QuaPatrolPlanParam;
import com.xinsoft.entity.po.QuaPatrolPlan;
import com.xinsoft.service.QuaPatrolPlanService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName QuaPatrolPlanController
 * @Description 巡检计划控制层
 * @Author lijie
 * @Date 2023-6-20 14:19:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/qua/partrol/plan")
@Api(value = "QuaPatrolPlanController", tags = {"质量管理-巡检计划控制层"})
public class QuaPatrolPlanController {
	@Autowired
	private QuaPatrolPlanService patrolPlanService;

	/**
	 * @Author Li9527
	 * @Description 巡检计划-暂存
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("暂存")
	@PostMapping("/add")
	public Response<?> add(@RequestBody QuaPatrolPlanDTO quaPatrolPlanDTO) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolPlanDTO);
		QuaPatrolPlan oldPlan = patrolPlanService.getOne(new QueryWrapper<QuaPatrolPlan>().eq("plan_name",quaPatrolPlanDTO.getPlanName()));
		if(oldPlan != null && !oldPlan.getId().equals(quaPatrolPlanDTO.getId())) {
			return Response.fail("该项巡检计划已存在,请不要重复添加");
		}
		patrolPlanService.tempSave(quaPatrolPlanDTO);
		return Response.succeed("操作成功");
	}

	/**
	 * @Author Li9527
	 * @Description 巡检计划-提交
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("提交")
	@PostMapping("/submit")
	public Response<?> submit(@RequestBody QuaPatrolPlanDTO quaPatrolPlanDTO) {
		// 参数校验
		ValidatorUtils.validateEntity(quaPatrolPlanDTO);
		QuaPatrolPlan oldPlan = patrolPlanService.getOne(new QueryWrapper<QuaPatrolPlan>().eq("plan_name",quaPatrolPlanDTO.getPlanName()));
		if(oldPlan != null && !oldPlan.getId().equals(quaPatrolPlanDTO.getId())){
			return Response.fail("该项巡检计划已存在,请不要重复添加");
		}
		patrolPlanService.submit(quaPatrolPlanDTO);
		return Response.succeed("操作成功");
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		QuaPatrolPlan plan = patrolPlanService.getByInfoById(id);
		return Response.succeed(plan);
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		for (int i = 0; i < ids.size(); i++) {
			QuaPatrolPlan plan = patrolPlanService.getById(ids.get(i));
			if(plan.getDoAble().equals(Const.AUDIT_STATUS_NORMAL)){
				return Response.fail("存在已启用的计划，无法删除");
			}
		}
		patrolPlanService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("批量启用")
	@PostMapping("/pick")
	public Response<?> pick(@RequestBody List<Integer> ids) {
		List<QuaPatrolPlan> list = new ArrayList<>();
		for (int i = 0; i < ids.size(); i++) {
			QuaPatrolPlan plan = new QuaPatrolPlan();
			plan.setId(ids.get(i));
			plan.setDoAble(Const.AUDIT_STATUS_NORMAL);
			list.add(plan);
		}
		patrolPlanService.updateBatchById(list);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("批量禁用")
	@PostMapping("/ban")
	public Response<?> ban(@RequestBody List<Integer> ids) {
		List<QuaPatrolPlan> list = new ArrayList<>();
		for (int i = 0; i < ids.size(); i++) {
			QuaPatrolPlan plan = new QuaPatrolPlan();
			plan.setId(ids.get(i));
			plan.setDoAble(Const.AUDIT_STATUS_BAN);
			list.add(plan);
		}
		patrolPlanService.updateBatchById(list);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 巡检计划分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody QuaPatrolPlanParam param) {
		return Response.succeed(patrolPlanService.findPageListByParam(param));
	}

}
